﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text;

public partial class Manage_TopicTree : System.Web.UI.Page
{
    protected string TreeText = "";

    CmsDataContext db = new CmsDataContext();

    protected void Page_Load(object sender, EventArgs ea)
    {
        //StringBuilder builder = new StringBuilder();
        //CmsDataContext db = new CmsDataContext();
        //var q = from aT in db.Topic
        //        where aT.ParentID == null
        //        select aT;

        //builder.Append("<div class=\"Tree\">");
        //builder.Append(string.Format("<div><table><tr><td style=\"padding-left:{1}px\"><a href=\"javascript:void(0);\" topicid=\"{2}\">{0}</a></td></tr></table></div>", "全部栏目", 0, "null"));
        //foreach (Topic t in q)
        //{
        //    builder.Append(CreateNode(t, 1));
        //}
        //builder.Append("</div>");

        //TreeText = builder.ToString();

        TreeNode tn = new TreeNode();
        tn.Text = "全部栏目";
        tn.Value = "";
        tn.NavigateUrl = "javascript:selectNode('0');";
        tn.Expanded = true;

        AddChilds(tn);

        TreeView1.Nodes.Add(tn);
    }

    protected void AddChilds(TreeNode pn)
    {
        List<Topic> ts;

        if (pn.Value == "")
        {
            ts = (from aT in db.Topic
                  where aT.ParentID == null
                  orderby aT.Name
                  select aT).ToList();
        }
        else
        {
            ts = (from aT in db.Topic
                  where aT.ParentID == pn.Value
                  orderby aT.Name
                  select aT).ToList();
        }

        foreach (var t in ts)
        {
            TreeNode tn = new TreeNode();
            tn.Text = t.Name;
            tn.Value = t.ID;
            tn.NavigateUrl = "javascript:selectNode('" + t.ID + "');";
            tn.Expanded = true;

            pn.ChildNodes.Add(tn);

            AddChilds(tn);
        }
    }
    protected string CreateNode(Topic topic, int level)
    {
        //StringBuilder sb = new StringBuilder();
        //sb.Append(string.Format("<div><table><tr><td style=\"padding-left:{1}px\"><a href=\"javascript:void(0);\" topicid=\"{2}\">{0}</a></td></tr></table></div>", topic.Name, level * 14, topic.ID));
        //if (topic.Child != null && topic.Child.Count > 0)
        //{
        //    sb.Append("<div>");

        //    foreach (Topic t in topic.Child)
        //    {
        //        sb.Append(CreateNode(t, level + 1));
        //    }

        //    sb.Append("</div>");
        //}

        //return sb.ToString();

        return "";
    }
}